/*
 Xern Entity Relocation Network
 Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
*/

#include "Packet.hpp"
#include "Message.hpp"
#include "EntityMessages.hpp"
#include "PlayerMessages.hpp"
#include "RegionMessages.hpp"
#include "ResourceMessages.hpp"
#include "SessionMessage.hpp"

namespace XernProtocol
{
	const u32 XernSignature = XERN_FOURCC('X', 'E', 'R', 'N');

	Message::Message()
	{
	}

	Message::~Message()
	{
	}

	u8 Message::GetFlags() const
	{
		return Packet::FL_RELIABLE;
	}

	u8 Message::GetChannel() const
	{
		return 0;
	}

	void Message::Decode(DataStream &in, size_t size)
	{
	}

	void Message::Encode(DataStream &out) const
	{
	}

	void Message::WriteToPacket(Packet *packet)
	{
		// Prepare the payload.
		packet->WritePayload(GetSize() + 6, NULL);

		// Create the encoder.
		DataStream out(packet->payloadSize, packet->payloadData);

		// Write the message signature and type.
		u16 type = GetType();
		out << XernSignature << type;

		// Write the message.
		Encode(out);
	}

	// Message decoder
	MessageDecoder::MessageDecoder()
	{
		// Build the message table.
		BuildMessageTable();
	}

	MessageDecoder::~MessageDecoder()
	{
	}

	boost::shared_ptr<IMessage> MessageDecoder::DecodeMessage(Packet *packet)
	{
		// Check for empty payload.
		if(packet->payloadSize < 6)
			return boost::shared_ptr<IMessage> ();

		// Create the payload reader.
		DataStream in(packet->payloadSize, packet->payloadData);

		// Read the message signature.
		u32 signature;
		in >> signature;

		// Check the signature.
		if(signature != XernSignature)
			return boost::shared_ptr<IMessage> ();

		// Read the message type.
		u16 type;
		in >> type;

		// Ignore nop messages.
		if(type == MT_NOP)
			return boost::shared_ptr<IMessage> ();

		// Find the message factory.
		MessageTable::const_iterator it = messageTable.find(type);
		if(it == messageTable.end())
			return boost::shared_ptr<IMessage> ();

		// Create the message and read it.
		boost::shared_ptr<Message> message = it->second->CreateMessage();
		message->Decode(in, packet->payloadSize-6);

		// Return the message.
		return message;
	}

	void MessageDecoder::BuildMessageTable()
	{
		// Sesssion messages
		AddMessage<SessionPingRequest> (MT_SESSION_PING_REQUEST);
		AddMessage<SessionPingResponse> (MT_SESSION_PING_RESPONSE);

		// Entity messages.
		AddMessage<EntityAdded> (MT_ENTITY_ADDED);
		AddMessage<EntityRemoved> (MT_ENTITY_REMOVED);
		AddMessage<EntityMovementUpdates> (MT_ENTITY_MOVEMENT_UPDATES);
		AddMessage<EntityPhysicUpdates> (MT_ENTITY_PHYSIC_UPDATES);
		AddMessage<EntityAddedResource> (MT_ENTITY_ADDED_RESOURCE);
		AddMessage<EntityRemovedResource> (MT_ENTITY_REMOVED_RESOURCE);

		// Player messages.
		AddMessage<PlayerEntityRequest> (MT_PLAYER_ENTITY_REQUEST);
		AddMessage<PlayerEntityResponse> (MT_PLAYER_ENTITY_RESPONSE);
		AddMessage<PlayerEntityEvent> (MT_PLAYER_ENTITY_EVENT);

		// Region messages.
		AddMessage<RegionListRequest> (MT_REGION_LIST_REQUEST);
		AddMessage<RegionListResponse> (MT_REGION_LIST_RESPONSE);
		AddMessage<RegionFullUpdateRequest> (MT_REGION_FULL_UPDATE_REQUEST);

		// Resource messages
		AddMessage<ResourceRequest> (MT_RESOURCE_REQUEST);
		AddMessage<ResourceRequestData> (MT_RESOURCE_REQUEST_DATA);
		AddMessage<ResourceData> (MT_RESOURCE_DATA);
		AddMessage<ResourceDescription> (MT_RESOURCE_DESCRIPTION);
		AddMessage<ResourceDenial> (MT_RESOURCE_DENIAL);
		AddMessage<ResourceState> (MT_RESOURCE_STATE);
	}
};
